package com.zhang.zhangapp.view.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.zhang.zhangapp.R;
import com.zhang.zhangapp.bean.user.Coupon;
import com.zhang.zhangapp.view.IndexFragment;
import com.zhang.zhangapp.view.shop.ShopDetailActivity;

import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.List;

/**
 * Created by Administrator on 2018/4/13.
 */

public class CashCouponAdapter extends BaseAdapter {

    private Context context;
    private List<Coupon> couponList;
    private String shopCode;

    public CashCouponAdapter(Context context, List<Coupon> couponList, String shopCode) {
        this.context = context;
        this.couponList = couponList;
        this.shopCode = shopCode;
    }

    @Override
    public int getCount() {
        return couponList == null ? 0 : couponList.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder vh;
        if (view == null) {
            vh = new ViewHolder();
            view = LayoutInflater.from(context).inflate(R.layout.item_cash_coupons, null);
            x.view().inject(vh, view);
            view.setTag(vh);
        } else {
            vh = (ViewHolder) view.getTag();
        }
        final Coupon coupon = couponList.get(i);
        vh.price.setText(coupon.getFaceval());
        vh.desc.setText(coupon.getDescr());
        vh.title.setText("代金券");
        vh.time.setText(coupon.getExp());
        vh.use.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(shopCode!=null && !shopCode.equals("")){
                    Activity activity = ((Activity) context);
                    activity.getIntent().putExtra("faceval", coupon.getFaceval());
                    activity.setResult(activity.RESULT_OK, activity.getIntent());
                    activity.finish();
                }else{
                    Intent intent = new Intent(context, ShopDetailActivity.class);
                    intent.putExtra("shopCode", coupon.getShopCode());
                    intent.putExtra("faceval",coupon.getFaceval());
                    context.startActivity(intent);
                }

            }
        });
        if (shopCode != null && !shopCode.equals("") && !shopCode.equals(coupon.getShopCode())) {
            vh.use.setOnClickListener(null);
            vh.use.setEnabled(false);
        }
        return view;
    }

    class ViewHolder {
        @ViewInject(R.id.item_cash_coupon_price)
        TextView price;
        @ViewInject(R.id.cash_coupon_title)
        TextView title;
        @ViewInject(R.id.cash_coupon_desc)
        TextView desc;
        @ViewInject(R.id.cash_coupon_time)
        TextView time;
        @ViewInject(R.id.item_use_coupon)
        TextView use;
    }
}
